﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Drawing;

namespace iOSRemoteConnector
{
    public class iOSDevice
    {
        //UUID Identifier 
        public String uuid {get; set;}

        //Received Files - Stored as a <filename,byte array> pair.
        public Dictionary<String, byte[]> receivedFiles {get; set;}

        //Downloading File Index - Because of the multipart nature of file downloads we need to store the index 
        public Dictionary<String,int> downloadingFileIndex { get; set; }

        //Socket for Server -> iOS Communication
        iOSSocket socket;


        public iOSDevice(iOSSocket s, String i)
        {

            uuid = i;

            socket = s;

            receivedFiles = new Dictionary<String, byte[]>();

            downloadingFileIndex = new Dictionary<String, int>();

        }


        public void SendCall(MethodCall mc) {

            socket.methodQueue.Enqueue(mc);
        
        }

        public void ReturnCall(String messageID) {
            //mc.hasReturned = true;
            //mc.OnReturnHandler(returnValue); 
        
        }

        public void ExceptionCall(String messageID) { }



        public void DisconnectDevice() { }



        /*File Handling Methods */

        public int GetIndexForFile(String fileName)
        {
            return downloadingFileIndex[fileName];

        }

        public void SetIndexForFile(String fileName, int index)
        {

            downloadingFileIndex[fileName] = index;


        }


        /*Special Socket Methods */
        public int GetPortNumber()
        {

            return socket.portNumber;


        }



        public void OnFileReceived(String filename)
        {

            //MemoryStream ms = new MemoryStream(receivedFiles[filename]);
            //Image myImage = Image.FromStream(ms); 
            

            FileStream fs = File.OpenWrite("C:\\Temp\\iPhoneData" + "\\" + filename); 
            byte[] imageAsArray = receivedFiles[filename]; 

            fs.Write(imageAsArray,0,imageAsArray.Length); 

            fs.Close(); 


        }
    }
}
